<template>
  <div class="login">
    <h2 style="font-family: KaiTi;font-size: 24px;">{{ $store.state.system.name }} - 登录</h2>
    <el-divider />
    <el-form ref="loginForm" :model="form" :rules="rules">
      <el-form-item prop="userName">
        <el-input v-model="form.userName" placeholder="请输入用户名" autocomplete="off" prefix-icon="el-icon-user" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" />
      </el-form-item>
      <el-form-item prop="authCode">
        <el-col :span="12">
          <el-input v-model="form.authCode" placeholder="请输入验证码" prefix-icon="el-icon-lock" />
        </el-col>
        <el-col :span="8" :offset="2">
          <img :src="authCodeBase64" alt="" title="看不清，换一张" @click="getCode()" />
        </el-col>
      </el-form-item>
      <el-button :loading="loading" round type="primary" @click="login" style="width: 60%">
        登 录
      </el-button>
    </el-form>

    <div class="beian">
      <a href="https://beian.miit.gov.cn/" target="_blank">晋ICP备2021003010号-2</a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      // 表单参数
      form: {
        userName: "",
        password: "",
        authCode: "",
      },
      authCodeBase64: "",
      // 表单校验
      rules: {
        userName: [{ required: true, message: "用户名不能为空!" }],
        password: [{ required: true, message: "密码不能为空!" }],
        authCode: [{ required: true, message: "验证码不能为空!" }],
      },
    };
  },
  beforeCreate() {
    let token = sessionStorage.getItem("token");
    if (token != null) {
      this.$router.push({ path: "/admin/index" });
      return;
    }
  },
  mounted() {
    this.getCode();
    window.addEventListener("keydown", this.keyDown, true);
  },
  methods: {
    keyDown(event) {
      if (event.keyCode === 13) {
        event.returnValue = false;
        this.login();
      }
    },
    getCode() {
      this.$api.auth.getCode().then((res) => {
        this.authCodeBase64 = res.data;
      });
    },
    login() {
      if(!this.$refs.loginForm){
        return;
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$api.auth.login(this.form).then((res) => {
            if (res.code == 200) {
              sessionStorage.setItem("token", res.data.token);
              sessionStorage.setItem("nickName", res.data.nickName);
              let redirect = this.$route.query.redirect;
              if(redirect){
                this.$router.push({ path: redirect });
              }else{
                this.$router.push({ path: "/admin/index" });
              }
              this.$message.success("登录成功");
            } else {
              this.getCode();
              this.$message.error(res.msg);
            }
            this.loading = false;
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 350px;
  height: 320px;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  text-align: center;
  // background: white;
  background: rgba(255, 255, 255, 0.6);
  padding: 40px;
  // border-bottom: 1px solid #ccc;
  border-radius: 10px;
  box-shadow: 0px 0px 5px #82cbff;
  input {
    background: rgba(255, 255, 255, 0.6);
    border-radius: 10px;
  }
  img{
    border-radius: 10px;
  }
  .beian {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100vw;
    background: rgba(255,255,255,0.3);
    height: 30px;
    line-height: 30px;
    a {
      color: #7a7979;
      text-decoration: none;
    }
  }
}
</style>